package B_FactoryMode.FactoryMode;

import java.util.Map;

/**
 * Created by zc on 2017/3/11.
 */
public class HairFactory {
    /**
     * 根据类型创建对象
     * @param type 发行类型
     * @return
     */
    public HairInterface getHair(String type) {
        if ("left".equals(type)) {
            return new LeftHair();
        } else if ("right".equals(type)) {
            return new LeftHair();
        }
        return null;
    }

    /**
     * 根据类名来生产对象
     * @param className 类名  注意 传入类的全路径名
     * @return
     */
    public HairInterface getHairByClass(String className) {
        HairInterface hair = null;
        try {
            hair = (HairInterface) Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return hair;
    }

    /**
     * 根据类名来生产对象
     * @param key 类名的对应简化词
     *      注意 类名为中文的时候会出现乱码 导致ClassNotFoundException
     * @return
     */
    public HairInterface getHairByClassKey(String key) {
        HairInterface hair = null;
        try {
            Map<String, String> map = PropertiesReader.class.newInstance().getProperties("type.properties");
            hair = (HairInterface) Class.forName(map.get(key)).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return hair;
    }
}
